/*
 * Copyright (C) 2010-2011 Graz University of Technology, Austria. All Rights reserved. 
 *
 * Contact: Simone Kopeinik <simone.kopeinik@tugraz.at>
 * 	   Graz University of Technology, Knowledge Management Institute, Brückenkopfgasse 1/6, 8020 Graz 	
 * 	   <http://www.kmi.tugraz.at/>
 * 
 * This software is part of the TARGET platform developed by the
 * TARGET EC-funded project www.reachyourtarget.org
 *
 * This software is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, version 3.
 *
 * This software is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 * GNU General Public License for more details.
 *
 * The GNU General Public License is available at <http://www.gnu.org/licenses/>.
 *
 */
package at.tugraz.kmi.css.analyzer.model;

import java.util.logging.Logger;

import javax.swing.tree.TreeModel;

import net.guts.gui.action.GutsAction;
import at.tugraz.kmi.css.analyzer.CompetenceAnalyzer;
import at.tugraz.kmi.css.analyzer.gapvis.JobProfile;
import at.tugraz.kmi.css.cbkst2.prereq.structurevis.CompetenceManagerModel;
import ca.odell.glazedlists.EventList;
import ca.odell.glazedlists.GlazedLists;
import ca.odell.glazedlists.swing.EventListModel;

import com.google.inject.Inject;
import com.jgoodies.binding.PresentationModel;

/**
 * 
 * @author Georg Öttl <georg.oettl@gmail.com>
 * 
 */
public class CompetenceAnalyzerModel extends
		PresentationModel<CompetenceAnalyzer> {

	private static final long							serialVersionUID			= 1L;

	public final static String							PROP_LOG_ENABLED			= "logEnabled";

	public final static String							PROP_STRUCTURE_VIEW_ENABLED	= "structureViewEnabled";

	private final CompetenceAnalyzer					domain;

	private final transient EventListModel<JobProfile>	jobProfileListModel;

	private boolean										logEnabled;

	private final CompetenceManagerModel				prerequesitesModel;

	private boolean										structureViewEnabled;

	private final transient GutsAction					updateGapAction;

	private final Logger								log;

	@Inject
	public CompetenceAnalyzerModel(
			CompetenceManagerModel prerequesitesPresentationModel,
			CompetenceAnalyzer cpaPresm, Logger log) {
		super(cpaPresm);
		this.log = log;
		domain = cpaPresm;
		prerequesitesModel = prerequesitesPresentationModel;
		EventList<JobProfile> jobProfilesEventList = GlazedLists
				.eventList(cpaPresm.getJobProfiles());
		jobProfileListModel = new EventListModel<JobProfile>(
				jobProfilesEventList);

		updateGapAction = new GutsAction("updateGap") {
			@Override
			protected void perform() {
				domain.updateGapGraph();
			}
		};
	}

	/**
	 * Shouldn't be here. Architectural hmpf...
	 * 
	 * @return the underlying domain model.
	 */
	public CompetenceManagerModel getPrerequesitesModel() {
		return prerequesitesModel;
	}

	/**
	 * returns the JobProfileListModel for the underlying job profiles
	 * 
	 * @return the JobProfile List model.
	 */
	public EventListModel<JobProfile> getJobProfileListModel() {
		return jobProfileListModel;
	}

	/**
	 * The tree model for a competence list
	 * 
	 * @return the tree model of a competence list
	 */
	public TreeModel getCompetenceList() {
		return prerequesitesModel.getCompetencesTreeModel();
	}

	/**
	 * Gets the update Gap Action
	 * 
	 * @return the update gap action.
	 */
	public GutsAction getUpdateGapAction() {
		return updateGapAction;
	}

	/**
	 * 
	 * getter for logenabled property
	 * 
	 * @return true if log is enabled otherwise false.
	 */
	public boolean isLogEnabled() {
		return logEnabled;
	}

	/**
	 * The structure ViewEnabled property.
	 * 
	 * @return true if structureview is enabled.
	 */
	public boolean isStructureViewEnabled() {
		return structureViewEnabled;
	}

	/**
	 * Set the logEnabled Property
	 * 
	 * @param logEnabled
	 *            true if log should be enabled in the view.
	 */
	public void setLogEnabled(boolean logEnabled) {
		boolean oldValue = this.logEnabled;
		this.logEnabled = logEnabled;
		log.info("Log Enabled:" + logEnabled);
		this.firePropertyChange(PROP_LOG_ENABLED, oldValue, this.logEnabled);
	}

	/**
	 * Setter for the StructerView property.
	 * 
	 * @param structureViewEnabled
	 *            true if the structure view should be enabled. false otherwise.
	 */
	public void setStructureViewEnabled(boolean structureViewEnabled) {
		boolean oldValue = this.structureViewEnabled;
		this.structureViewEnabled = structureViewEnabled;
		log.info("StructureView :" + structureViewEnabled);
		this.firePropertyChange(PROP_STRUCTURE_VIEW_ENABLED, oldValue,
				this.structureViewEnabled);
	}

}
